﻿using Dapper;
using System;
using BCEntity.Common.Cooperation.WebsiteCooperation;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Cooperation.WebsiteCooperation
{
    /// <summary>
    /// 官网合作留言
    /// </summary>
    public class WebsiteCooperationData : IWebsiteCooperationData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitecooperation";
        private const string QUERY = @"SELECT WebsiteCooperationId,FollowUpTimes,UpdateTime,ContactPerson,Company,ContactPhone,JobTitle,Email,CooperationContent,CreateTime,HasFollowUp FROM websitecooperation";
        public WebsiteCooperationData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        #region 检查官网合作留言是否存在
        public async Task<bool> Exists(int websiteCooperationId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteCooperationId=?WebsiteCooperationId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCooperationId", websiteCooperationId, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 检查官网合作留言是否存在
        public async Task<bool> Exists(string company)
        {
            string sql = string.Format(@"{0} WHERE Company=?Company LIMIT 1", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?Company", company, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网合作留言
        public async Task<WebsiteCooperationEntity> Add(WebsiteCooperationEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitecooperation", new string[] { "ContactPerson", "Company", "ContactPhone", "Email", "JobTitle", "CooperationContent" });
            sql = string.Format("{0} {1} WHERE WebsiteCooperationId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?ContactPerson", entity.ContactPerson?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Company", entity.Company?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ContactPhone", entity.ContactPhone?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Email", entity.Email?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?JobTitle", entity.JobTitle?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?CooperationContent", entity.CooperationContent?.Trim(), DbType.String);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteCooperationEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改官网合作留言
        public async Task<WebsiteCooperationEntity> Update(WebsiteCooperationEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websitecooperation", new string[] { "FollowUpTimes", "ContactPerson", "Company", "ContactPhone", "JobTitle", "Email", "CooperationContent", "HasFollowUp", "FollowUpTimes" }, new string[] { "WebsiteCooperationId" });
            sql = string.Format("{0} {1} WHERE WebsiteCooperationId=?WebsiteCooperationId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCooperationId", entity.WebsiteCooperationId, DbType.Int32, size: 11);
            parameters.Add("?FollowUpTimes", entity.FollowUpTimes, DbType.Int32, size: 11);
            parameters.Add("?ContactPerson", entity.ContactPerson?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Company", entity.Company?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?ContactPhone", entity.ContactPhone?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?JobTitle", entity.JobTitle?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Email", entity.Email?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?CooperationContent", entity.CooperationContent?.Trim(), DbType.String);
            parameters.Add("?HasFollowUp", entity.HasFollowUp, DbType.Boolean);
            parameters.Add("?FollowUpTimes", entity.FollowUpTimes, DbType.Int32);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteCooperationEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除官网合作留言
        public async Task<bool> Delete(int websiteCooperationId)
        {
            string sql = @"DELETE FROM websitecooperation WHERE WebsiteCooperationId=?WebsiteCooperationId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCooperationId", websiteCooperationId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网合作留言
        public async Task<WebsiteCooperationEntity> GetEntity(int websiteCooperationId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteCooperationId=?WebsiteCooperationId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteCooperationId", websiteCooperationId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WebsiteCooperationEntity>(sql, parameters);
        }
        #endregion

        #region 获取官网合作留言列表
        public async Task<IEnumerable<WebsiteCooperationEntity>> GetList()
        {
            return await this.databaseContext.Slave.QueryAsync<WebsiteCooperationEntity>(QUERY);
        }
        #endregion

        #region 获取官网合作留言分页
        public async Task<(IEnumerable<WebsiteCooperationEntity>, int)> Query(bool? hasFollowUp, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (hasFollowUp.HasValue)
            {
                wheres.Add("HasFollowUp=?HasFollowUp");
                parameters.Add("?HasFollowUp", hasFollowUp.Value, DbType.Boolean);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websitecooperation {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteCooperationEntity, int>(sql, parameters);
        }
        #endregion
    }
}



